import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/', redirect:'/manager/home'},
    {
      path: '/manager',  component: ()=>import('../views/Manager.vue'),
      children:[
        { path: 'home', meta:{name:'主页'}, component:()=> import('../views/manager/Home.vue')},
        { path: 'admin', meta:{name:'管理员信息', requiresAdmin: true}, component: ()=>import('../views/manager/Admin.vue')},
        { path: 'user', meta:{name:'普通用户信息', requiresAdmin: true}, component: ()=>import('../views/manager/User.vue')},
        { path: 'person',meta:{name:'个人信息'}, component: ()=>import('../views/manager/person.vue')},
        { path: 'updatePassword',meta:{name:'修改密码'}, component: ()=>import('../views/manager/UpdatePassword.vue')},
        { path: 'notice',meta:{name:'系统公告'}, component: ()=>import('../views/manager/Notice.vue')},
        { path: 'introduction',meta:{name:'帖子'}, component: ()=>import('../views/manager/Introduction.vue')},
        { path: 'category',meta:{name:'图书分类'}, component: ()=>import('../views/manager/Category.vue')},
        { path: 'apply',meta:{name:'预约申请'}, component: ()=>import('../views/manager/Apply.vue')},
        { path: 'book',meta:{name:'图书信息'}, component: ()=>import('../views/manager/Book.vue')},
        { path: 'record',meta:{name:'借阅信息'}, component: ()=>import('../views/manager/Record.vue')},
        { path: 'postsSection',meta:{name:'社区板块'}, component: ()=>import('../views/manager/PostsSection.vue')},
        { path: 'activity',meta:{name:'图书活动'}, component: ()=>import('../views/manager/Activity.vue')},
        { path: 'room',meta:{name:'阅览室'}, component: ()=>import('../views/manager/Room.vue')},
        { path: 'seat',meta:{name:'座位信息'}, component: ()=>import('../views/manager/Seat.vue')},
        { path: 'activityRecord',meta:{name:'活动报名消息'}, component: ()=>import('../views/manager/ActivityRecord.vue')},
      ]
    },
    {
      path: '/front',  component: ()=>import('../views/Front.vue'),
      children:[
        { path: 'home',  component: import('../views/front/Home.vue')},
        { path: 'introductionDetail',  component: import('../views/front/IntroductionDetail.vue')},
        { path: 'person',  component: import('../views/front/Person.vue')},
        { path: 'updatePassword',  component: import('../views/front/UpdatePassword.vue')},
        { path: 'bookDetail',  component: import('../views/front/BookDetail.vue')},
        { path: 'myRecord',  component: import('../views/front/MyRecord.vue')},
        { path: 'categoryBook',  component: import('../views/front/CategoryBook.vue')},
        { path: 'activityDetail',  component: import('../views/front/ActivityDetail.vue')},
        { path: 'activity',  component: import('../views/front/Activity.vue')},
        { path: 'roomDetail',  component: import('../views/front/roomDetail.vue')},
        { path: 'room',  component: import('../views/front/Room.vue')},
        { path: 'mySeat',  component: import('../views/front/MySeat.vue')},
      ]
    },
    {
      path: '/login',  component: import('../views/Login.vue')
    },
    {
      path: '/register',  component: import('../views/Register.vue')
    },
    {
      path: '/notFound',  component: import('../views/404.vue')
    },
    {
      path: '/:pathMath(.*)',  redirect:'/notFound'
    },
  ],
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  const user = JSON.parse(localStorage.getItem('code_user') || '{}');
  const isAdmin = user.role === 'ADMIN';

  if (to.meta.requiresAdmin &&!isAdmin) {
    // 如果需要管理员权限且当前用户不是管理员，重定向到登录页
    next('/notFound');
  } else {
    next();
  }
});

export default router